﻿using System;
#if CONTRACTS_FULL
using System.Diagnostics.Contracts;
#endif
using System.Windows.Input;

namespace Elca.MvvmHelpers {

    /// <summary>
    /// Simple command that is always executable
    /// </summary>
    public class ExecutableCommand : ICommand {

        private readonly Action<object> _action;

        /// <summary>
        /// Constructor taking a delegate with parameter
        /// </summary>
        /// <param name="action">The action to perform when the command is executed</param>
        public ExecutableCommand(Action<object> action) {

            _action = action;
        }

        /// <summary>
        /// Constructor taking a delegate without any parameter
        /// </summary>
        /// <param name="action">The action to perform when the command is executed</param>
        public ExecutableCommand(Action action) {

            _action = delegate { action(); };
        }

        /// <summary>
        /// Implementation of <c>ICommand.Execute</c>
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(object parameter) {

            _action(parameter);
        }

        /// <summary>
        /// Implemenation of <c>ICommand.CanExecute</c>
        /// </summary>
        /// <param name="parameter">parameter, typically from XAML</param>
        /// <returns>True if the command is enabled</returns>
        public virtual bool CanExecute(object parameter) {
            return true;
        }

        /// <summary>
        /// Implemenation of <c>ICommand.CanExecute</c>
        /// </summary>
        public event EventHandler CanExecuteChanged {
            // see: http://joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/

            add {
                CommandManager.RequerySuggested += value;
            }
            remove {
                CommandManager.RequerySuggested -= value;
            }
        }
    }

    /// <summary>
    /// A command that determines if it is enabled through a delegate
    /// </summary>
    public class ConditionalCommand : ExecutableCommand {

        private readonly Func<bool> _validation;

        /// <summary>
        /// Constructor taking 2 delegates, one with an action parameter
        /// </summary>
        /// <param name="action">The action to execute</param>
        /// <param name="validation">Determines if the command is enabled</param>
        /// <param name="dependentProperties">The list of properties whose change implies a change in <c>CanExecute</c></param>
        public ConditionalCommand(Action<object> action, Func<bool> validation, params string[] dependentProperties) : base(action) {

#if CONTRACTS_FULL
            Contract.Requires(validation != null);
#endif
            _validation = validation;
        }

        /// <summary>
        /// Constructor taking 2 delegates without parameter
        /// </summary>
        /// <param name="action">The action to execute</param>
        /// <param name="validation">Determines if the command is enabled</param>
        /// <param name="dependentProperties">The list of properties whose change implies a change in <c>CanExecute</c></param>
        public ConditionalCommand(Action action, Func<bool> validation, params string[] dependentProperties)
            : base(action) {

#if CONTRACTS_FULL
            Contract.Requires(validation != null);
#endif
            _validation = validation;
        }

        /// <summary>
        /// Is the command enabled ?
        /// </summary>
        /// <param name="parameter">Ignored parameter</param>
        /// <returns>True if the command is enabled</returns>
        public override bool CanExecute(object parameter) {
            return _validation();
        }
    }
}
